package com.wlz.security.service;

import com.wlz.security.config.WebSecurityConfig2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

/**
 *  方式 3
 *   用户密码 加密  使用 BCrypt 默认
 */
@ConditionalOnBean(WebSecurityConfig2.class)
@Service
public class MyUserDetailsService2 implements UserDetailsService {


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        // 使用 BCrypt ,但是不建议这么写，不容易扩展， 可以使用自定义编码器实现
        String pw = BCrypt.hashpw("123456", BCrypt.gensalt());
        UserDetails userDetails = new User("wlz", "{bcrypt}" + pw,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));

        return userDetails;
    }
}
